package com.luiz.aero.android.main;


import com.luiz.aero.android.computation.RouteComputation;
import com.luiz.aero.android.connection.BluetoothManager;
import com.luiz.aero.android.main.R;
import com.luiz.aero.android.sensor.AccelerometerListener;
import com.luiz.aero.android.sensor.GPSListener;
import com.luiz.aero.android.sensor.OrientationListener;


import android.app.Activity;
import android.content.Context;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{

	private BluetoothManager btManager;

	private Button connectBt = null;
	private EditText deviceNameTxt = null;
	private TextView acelerometerSensorTx = null;
	private TextView orientationSensorTx = null;
	private TextView gpsTx = null;
	private TextView gpsTitle = null;
	private Button startMonitoringBt;
	private Button startRecordingRouteBt;
	private Button stopRecordingRouteBt;
	private CheckBox isConnectedCB = null;

	private SensorManager sensorManager;
	private AccelerometerListener accListener;
	private OrientationListener oriListener;
	private GPSListener gpsListener;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		connectBt = (Button) findViewById(R.id.connectBt);
		deviceNameTxt = (EditText) findViewById(R.id.text);
		isConnectedCB = (CheckBox) findViewById(R.id.isBluetoothOkCB);
		startMonitoringBt = (Button) findViewById(R.id.startMonitoringBt);
		startRecordingRouteBt = (Button) findViewById(R.id.startRecordingRouteBt);
		stopRecordingRouteBt = (Button) findViewById(R.id.stopRecordingRouteBt);
		gpsTx = (TextView) findViewById(R.id.gpsTxtLoc);
		gpsTitle = (TextView) findViewById(R.id.gpsTxt);
		acelerometerSensorTx = (TextView) findViewById(R.id.accelerometerSensorLb);
		orientationSensorTx = (TextView) findViewById(R.id.orientationSensorLb);


		//SETTING BLUETOOTH CONNECTION
		connectBt.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setBluetoothConnection();
			}
		});

		startMonitoringBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setSensors();
			}
		});

		startRecordingRouteBt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				System.setProperty("is-flying", "true");
				RouteComputation comp = new RouteComputation(getApplicationContext());
				comp.start();
			}
		});
		
		stopRecordingRouteBt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				System.setProperty("is-flying", "false");
			}
		});
	}

	private void setBluetoothConnection() {

		try {
			String deviceName = deviceNameTxt.getText().toString();
			if(deviceName.equals("")){
				Toast.makeText(this, "The Bluetooth device name is invalid.", 2000);
			}else{
				btManager = new BluetoothManager();
				boolean connected = btManager.setBluetoothConnection(deviceName);
				isConnectedCB.setChecked(connected);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(this, "The Bluetooth device cannot be found.", 2000);
		}
	}



	private void setSensors() {
		gpsListener = new GPSListener(gpsTx, gpsTitle);
		gpsListener.setGPSConnection(this);
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		accListener = new AccelerometerListener(sensorManager, acelerometerSensorTx);
		oriListener = new OrientationListener(sensorManager, orientationSensorTx);

	}

}
